package com.ddwl.order.controller;


import com.ddwl.common.annotation.Auth;
import com.ddwl.common.annotation.Submit;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.order.service.SeckillService;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.order.CreateSeckillBo;
import com.ddwl.schema.bo.order.CreateSeckillItemBo;
import com.ddwl.schema.bo.order.QuerySeckillBo;
import com.ddwl.schema.bo.order.SubmitSeckillItemBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.order.SeckillItemVo;
import com.ddwl.schema.vo.order.SeckillVo;
import com.ddwl.schema.vo.order.SecondKillOrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 类SeckillController.java的实现描述：秒杀相关接口
 *
 * @author wenbin
 */
@Api(tags = "秒杀服务")
@Slf4j
@RequestMapping("/secKill")
@RestController
public class SeckillController extends BaseController {

    @Resource
    private SeckillService seckillService;

    @Submit
    @PostMapping("/createSeckill")
    @Auth(product = {BaseEnum.ProductEnum.CONSOLE})
    @ApiOperation("创建秒杀活动")
    public RestVo<SeckillVo> createSeckill(@Valid @RequestBody CreateSeckillBo bo) {
        SeckillVo seckillVo = seckillService.createSeckill(bo);
        return RestVo.ok(seckillVo);
    }

    @PostMapping("/updateSeckill")
    @Auth(product = {BaseEnum.ProductEnum.CONSOLE})
    @ApiOperation("修改秒杀活动")
    public RestVo<SeckillVo> updateSeckill(@Valid @RequestBody CreateSeckillBo bo) {
        SeckillVo seckillVo = seckillService.updateSeckill(bo);
        return RestVo.ok(seckillVo);
    }

    @PostMapping("/updateSeckillStatus")
    @Auth(product = {BaseEnum.ProductEnum.CONSOLE})
    @ApiOperation("修改秒杀活动状态")
    public RestVo<SeckillVo> updateSeckillStatus(@RequestBody QuerySeckillBo bo) {
        seckillService.updateSeckillStatus(bo.getSeckillId(), bo.getIsEffect());
        return RestVo.ok();
    }

    @DeleteMapping("/deleteSeckill/{seckillId}")
    @Auth(product = {BaseEnum.ProductEnum.CONSOLE})
    @ApiOperation("删除秒杀活动")
    public RestVo deleteSeckill(@PathVariable String seckillId) {
        seckillService.deleteSeckill(seckillId);
        return RestVo.ok();
    }

    @PostMapping("/querySeckill")
    //@Auth(product={BaseEnum.ProductEnum.CONSOLE,BaseEnum.ProductEnum.FENXIAO,BaseEnum.ProductEnum.MALL})
    @ApiOperation("查询秒杀活动")
    public RestVo<PageVo<SeckillVo>> querySeckill(@RequestBody PageBo<QuerySeckillBo> pageBo) {
        return RestVo.ok(seckillService.getSeckillPage(pageBo));
    }

    @PostMapping("/querySeckillNow")
    @ApiOperation("查询当前进行的秒杀活动")
    public RestVo<SeckillVo> getSeckillNow() {
        return RestVo.ok(seckillService.getSeckillNow());
    }


    @PostMapping("/createSeckillItem")
    @Auth(product = {BaseEnum.ProductEnum.CONSOLE, BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.MALL})
    @ApiOperation("添加秒杀商品")
    public RestVo createSeckillItem(@Valid @RequestBody List<CreateSeckillItemBo> bo) {
        seckillService.createSeckillItem(bo);
        return RestVo.ok();
    }

    @PostMapping("/updateSeckillItem")
    @Auth(product = {BaseEnum.ProductEnum.CONSOLE})
    @ApiOperation("修改秒杀商品")
    public RestVo updateSeckillItem(@RequestBody List<CreateSeckillItemBo> bo) {
        seckillService.updateSeckillItem(bo);
        return RestVo.ok();
    }

    @DeleteMapping("/deleteSeckillItem/{seckillItemId}")
    @Auth(product = {BaseEnum.ProductEnum.CONSOLE})
    @ApiOperation("删除秒杀商品")
    public RestVo deleteSeckillItem(@PathVariable String seckillItemId) {
        seckillService.deleteSeckillItem(seckillItemId);
        return RestVo.ok();
    }

    @PostMapping("/querySeckillItem")
    //@Auth(product={BaseEnum.ProductEnum.CONSOLE,BaseEnum.ProductEnum.FENXIAO,BaseEnum.ProductEnum.MALL})
    @ApiOperation("查询秒杀商品")
    public RestVo<PageVo<SeckillItemVo>> querySeckillItem(@RequestBody PageBo<QuerySeckillBo> pageBo) {
        return RestVo.ok(seckillService.getSeckillItemPage(pageBo));
    }

    @PostMapping("/querySeckillItemDetail")
    @Auth(product = {BaseEnum.ProductEnum.CONSOLE, BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("查询秒杀商品(商品明细用)")
    public RestVo<List<SeckillItemVo>> querySeckillItemDetail(@RequestBody QuerySeckillBo bo) {
        return RestVo.ok(seckillService.getSeckillItemDetail(bo));
    }

    @PostMapping("/querySeckillItemList")
    @Auth(product = {BaseEnum.ProductEnum.API})
    @ApiOperation("查询秒杀商品列表(收藏夹用)")
    public RestVo<List<SeckillItemVo>> querySeckillItemList(@RequestBody QuerySeckillBo bo) {
        return RestVo.ok(seckillService.getSeckillItemList(bo));
    }

    @Submit
    @PostMapping("/createSeckillOrder")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("创建秒杀订单")
    public RestVo<String> createSeckillOrder(@Valid @RequestBody SubmitSeckillItemBo bo) {
        String userId = getUserId();
        String product = getProduct();
        String userName = getUserNameNew();
        return RestVo.ok(seckillService.createSeckillOrder(bo, userId, product, userName));
    }

    @PostMapping("/getSeckillOrder")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.API, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("查询秒杀订单")
    public RestVo<SecondKillOrderVo> createSeckillOrder(@RequestParam String id) {
        return RestVo.ok(seckillService.querySecondKillOrder(id));
    }

}
